package com.iflytek.yly.jetpackdemo.viewmodel;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.iflytek.yly.jetpackdemo.R;

public class TimerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewmodel_timer);
        initComponent();
    }

    private void initComponent(){

        final TextView tvTime = findViewById(R.id.tvTime);
        TimerViewModel timerViewModel = new ViewModelProvider(this).get(TimerViewModel.class);

        timerViewModel.setOnTimeChangeListener(
                second -> runOnUiThread(() -> tvTime.setText(("Time : " + second))));
        timerViewModel.startTiming();
    }

}
